package com.hezhou.hezhouesjd.controller;

import com.hezhou.hezhouesjd.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * ---------------------------
 * (ContentController) jd业务请求层
 * ---------------------------
 *
 * @Author: [hezhou]
 * @Date: 2020/5/14
 * @Version: [1.0.1]
 * ---------------------------
 */
@Controller
public class ContentController {

    @Autowired
    private ContentService contentService;

    @GetMapping("/parse/{keyword}")
    public void parse(@PathVariable("keyword") String keyword, HttpServletRequest request,HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        if (contentService.parseContent(keyword)) {
            response.getWriter().write("插入成功！");
        }else{
            response.getWriter().write("插入失败！");
        }
    }

    /**
     * 搜索
     * @param keyword 搜索的关键字
     * @param pageNo 页标
     * @param pageSize 每页的条数
     * @return
     * @throws IOException
     */
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    @ResponseBody
    public List<Map<String,Object>> search(
            @PathVariable("keyword")String keyword,
            @PathVariable("pageNo") int pageNo,
            @PathVariable("pageSize") int pageSize
    ) throws IOException {
        List<Map<String, Object>> list = contentService.searchPage(keyword, pageNo, pageSize);
        return list;
    }

}
